package wu.dao;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import wu.pojo.User_niu;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ReadTxtDao {
    public static List<User_niu> getniu_User(int niu_Number, List<String> filelist) {
//        List<String> filelist = readfile(niu_Number);
        List<User_niu> userlist = new ArrayList<>();
//        int size=(new Scanner(System.in)).nextInt();
        int size = filelist.size();
        for (int i = 0, j = size / 2; i < size / 2; i++, j++) {
            User_niu user = new User_niu(-1,null, null, null, null, null,null,-1,-1,-1,-1,-1,-1,-1);
            String s1 = filelist.get(i), s2 = filelist.get(j);
            String[] strings1 = s1.split(" "), strings2 = s2.split(" ");
//            double[] score = new double[strings2.length/2];
            List<Double> score = new ArrayList<>();
            for (int iv = 0, kk = 0; iv < strings2.length; iv++) {//************题目数据处理-1*****************
                if (strings2[iv].equals("-1")) {
                    if (iv == 0) {
//                        score[kk] = -1;
                        score.add(-1.0);
                    } else {
//                        score[kk] = Double.parseDouble(strings2[iv - 1]);
                        score.add(Double.parseDouble(strings2[iv - 1]));
                    }
                    kk++;
                }
            }
            user.setNiu_Ranking(strings1[0]);//比赛总名次
            user.setNiu_id(Integer.parseInt(strings1[1]));//牛客id
            user.setNiu_Contestants(strings1[2]);//牛客名称
            user.setNiu_School(strings1[3]);//学校
            user.setNiu_AC(strings1[4]);//AC数
            user.setNiu_total_score(strings1[5]);//总分
            if (strings1.length == 6) {
                user.setNiu_time("00:00:00");//用时
            } else {
                user.setNiu_time(strings1[6]);//用时
            }
            user.setA_score(score.get(0));
            user.setB_score(score.get(1));
            user.setC_score(score.get(2));
            if (score.size() > 3)
                user.setD_score(score.get(3));
            if (score.size() > 4)
                user.setE_score(score.get(4));
            if (score.size() > 5)
                user.setF_score(score.get(5));
            if (score.size() > 6)
                user.setG_score(score.get(6));
            userlist.add(user);
        }

        return userlist;
    }

    /**
     * @param niuNumber 周赛数字
     * @return 排名成绩数据List<String>  1.处理 待判题数量的0 改成-1 2.处理牛客的id
     * 调用readFileToString返回文件字符串
     * 调用extractContent返回html渲染的数据
     */
    public static List<String> readfile(int niuNumber) {
        String filePath = "src/main/resources/niu_number_txtfile/niu_" + niuNumber + "_txt";
        String fileString = readFileToString(filePath);
//        String fileString2=fileString;
        String fileString2 = null;
        if (fileString != null) {
            fileString2 = fileString.replaceAll("none;\">0</div></td>", "none;\">-1</div></td>");
            fileString2 = fileString2.replaceAll("none;\">\n" + "       0\n" + "      </div>", "none;\">-1</div></td>");
            fileString2 = fileString2.replaceAll("<a href=\"/acm/contest/profile/", "<p >");
            fileString2 = fileString2.replaceAll("\\\" target\\=\\\"_blank\\\" title\\=", "</p><a href=\"\" target=\"_blank\" title=");
        } else {
            System.out.println("fileString2 = null");
        }
        ;//处理 待判题数量的0 改成-1
        List<String> result = extractContent(fileString2);
        return result;
    }

    /**
     * @return 返回html渲染的数据
     */
    private static List<String> extractContent(String html) {
        List<String> contentList = new ArrayList<>();
        Document doc = Jsoup.parse(html);
        Elements tableRows = doc.select("table tbody tr");
        for (Element row : tableRows) {
            Elements cells = row.select("td");
            StringBuilder rowContent = new StringBuilder();
            for (Element cell : cells) {
                String text = cell.text();
                rowContent.append(text).append(" ");
            }
            contentList.add(rowContent.toString().trim());
        }
        return contentList;
    }

    /**
     * @return 文件内容的字符串
     */
    static String readFileToString(String filePath) {
        StringBuilder content = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                content.append(line).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return content.toString();
    }


}
